﻿using cszmcaux;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LKLCode.MotionControlCard.Zmotion
{
    internal class ZmcMotion : IMotion
    {
        private string ip;//本卡IP
        private IntPtr zmc_Handle;//控制卡句柄
        private StringBuilder ControllerId = new StringBuilder(),SoftType = new StringBuilder(), SoftVersion = new StringBuilder();

        /// <summary>
        /// ZMC轴列表
        /// </summary>
        public Test_IAxis[] Axises { get { return _axises; } }
        public Test_IAxis[] _axises = Array.Empty<Test_IAxis>();
        //_axises被初始化为空数组，因此在InitializeAxes方法中，
        //可以安全地检查数组的长度，而无需担心_axises为null。

        /// <summary>
        /// 运动卡状态
        /// </summary>
        public MotionStatus MotionStatus
        {
            get
            {
                return motionStatus;
            }
        }
        private MotionStatus motionStatus;

        public ZmcMotion()
        {
            motionStatus = new MotionStatus() { IsEmergentStop = false, IsPause = false, IsConnected = false };
        }

        /// <summary>
        /// 初始化所有轴
        /// </summary>
        /// <param name="axisesInitType"></param>
        /// <param name="axisTypes">轴类型列表，用于指定每个轴的类型</param>
        public void InitAllAixs(AxisInitType[] axisesInitType)
        {
            if(!motionStatus.IsConnected)
            {
                return;
            }
            _axises = new Test_IAxis[axisesInitType.Length];
            for(int i = 0;i<axisesInitType.Length;i++)
            {
                Test_IAxis axis = new ZmcAxis(zmc_Handle, axisesInitType[i]);
                _axises[i] = axis;
            }
        }

        /// <summary>
        /// 急停
        /// </summary>
        public void EmergenStop()
        {
            motionStatus.IsEmergentStop = true;
            foreach(var axis in _axises)
            {
                axis.EmergentStop();
            }            
        }

        /// <summary>
        /// 解除运动控制卡急停
        /// </summary>
        public void ResetEmergenStop()
        {
            motionStatus.IsEmergentStop = true;
            foreach(var axis in _axises)
            {
                axis.EmergentStop();
            }
        }

        /// <summary>
        /// 断开控制器连接
        /// </summary>
        public void Close()
        {
            if(!motionStatus.IsConnected)
            {
                return;
            }
            zmcaux.ZAux_Close(zmc_Handle);
        }

        /// <summary>
        /// 获取运动卡ID号
        /// </summary>
        /// <returns></returns>
        public string GetMotioCardId()
        {
            if(ControllerId.Length == 0)
            {
                GetMotioCardId();
            }
            return ControllerId.ToString(); 
        }

        private void GetControllerInfo()
        {
            if(!motionStatus.IsConnected)
            {
                throw new Exception("zmc Link fail");
            }
            zmcaux.ZAux_GetControllerInfo(zmc_Handle, SoftType, SoftVersion, ControllerId);
        }


        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            if(motionStatus.IsEmergentStop == true)
            {
                return;
            }
            motionStatus.IsPause = true;
            foreach(var axis in _axises)
            {
                axis.Pause();
            }
        }

        /// <summary>
        /// 重新开始
        /// </summary>
        public void Resume()
        {
            if(motionStatus.IsEmergentStop == true )
            {
                return;
            }
            foreach(var axis in _axises)
            {
                axis.Resume();
            }
            motionStatus.IsPause = false;
        }









    }
}
